package cn.xorange.commons.file.utils;

import cn.xorange.commons.file.configuration.FileConstants;
import cn.xorange.commons.file.configuration.MimeTypeConstants;
import cn.xorange.commons.file.entity.FileCode;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class FileUtil {
    private FileUtil(){}

    /**
     * 默认类型上传路径
     * avatar :/avatar/202308/
     * image  :/image
     * media  :/media
     * video  :/video
     * import :/import
     * 具体的路径
     * File.separator 的作用等同于 / （linux 下文件路劲分割符必须为 / windows为 \ 但 不区分 \ /
     */
    public static String getUploadPath(String dir){
        String monthDr = new SimpleDateFormat("yyyyMM").format(new Date());
        if(FileConstants.DEFAULT_AVATAR_PATH.equals(dir)){
            return FileConstants.DEFAULT_AVATAR_PATH + "/" + monthDr + "/";
        }else if (FileConstants.DEFAULT_PREVIEW_PATH.equals(dir)) {
            return FileConstants.DEFAULT_PREVIEW_PATH + "/" + monthDr + "/";
        }else if (FileConstants.DEFAULT_IMAGE_PATH.equals(dir)){
                return FileConstants.DEFAULT_IMAGE_PATH + "/" + monthDr + "/";
        }else if (FileConstants.DEFAULT_MEDIA_PATH.equals(dir)){
            return FileConstants.DEFAULT_MEDIA_PATH + "/" + monthDr + "/";
        }else if (FileConstants.DEFAULT_VIDEO_PATH.equals(dir)){
            return FileConstants.DEFAULT_VIDEO_PATH + "/" + monthDr + "/";
        }else if (FileConstants.DEFAULT_IMPORT_PATH.equals(dir)){
            return FileConstants.DEFAULT_IMPORT_PATH;
        }else {
            return dir + "/";
        }
    }

    /**
     * 文件名称验证
     *
     * @param filename 文件名称
     * @return true 正常 false 非法
     */
    public static boolean checkValidFilename(String filename) {
        return filename.matches(FileConstants.FILENAME_PATTERN);
    }

    /**
     * 根据上传目录确认文件上传类型
     * @param dir
     * @return
     */
    public static String[] getUploadSuffix(String dir){
        if(FileConstants.DEFAULT_AVATAR_PATH.equals(dir)) {
            return MimeTypeConstants.AVATAR_EXTENSION;
        }else if (FileConstants.DEFAULT_PREVIEW_PATH.equals(dir)){
                return MimeTypeConstants.AVATAR_EXTENSION;
        }else if (FileConstants.DEFAULT_IMAGE_PATH.equals(dir)){
            return MimeTypeConstants.IMAGE_EXTENSION;
        }else if (FileConstants.DEFAULT_MEDIA_PATH.equals(dir)){
            return MimeTypeConstants.MEDIA_EXTENSION;
        }else if (FileConstants.DEFAULT_VIDEO_PATH.equals(dir)){
            return MimeTypeConstants.VIDEO_EXTENSION;
        }else {
            return MimeTypeConstants.DEFAULT_ALLOWED_EXTENSION;
        }
    }

    /**
     * 校验上传文件的名称、大小以及文件格式
     *
     * @param file 上传的文件
     * @return
     */
    public static FileCode checkFileNameAndExtend(MultipartFile file, String[] allowedExtension) {
        if(Objects.isNull(file)){
            return FileCode.FILE_NULL;
        }
        // 检查文件名长度
        String fileName = file.getOriginalFilename();
        if(Objects.isNull(fileName)){
            return FileCode.FILE_NAME_LENGTH;
        }else {
            if (fileName.length() > FileConstants.DEFAULT_FILE_NAME_LENGTH) {
                return FileCode.FILE_NAME_LENGTH;
            }
        }
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if (Objects.nonNull(allowedExtension) && !checkAllowedExtendName(extension, allowedExtension)) {
            FileCode code = FileCode.FILE_EXTENSION_EXCEPTION;
            if (allowedExtension == MimeTypeConstants.IMAGE_EXTENSION) {
                code.setMsg("图片" + code.getMsg());
                return code;
            }
            else if (allowedExtension == MimeTypeConstants.FLASH_EXTENSION) {
                code.setMsg("flash" + code.getMsg());
                return code;
            }
            else if (allowedExtension == MimeTypeConstants.MEDIA_EXTENSION) {
                code.setMsg("媒体" + code.getMsg());
                return code;
            }
            else if (allowedExtension == MimeTypeConstants.VIDEO_EXTENSION) {
                code.setMsg("视频" + code.getMsg());
                return code;
            }else if(allowedExtension == MimeTypeConstants.DEFAULT_ALLOWED_EXTENSION){
                return code;
            } else {
                return code;
            }
        }else {
            return null;
        }
    }

    /**
     * 判断MIME类型是否是允许的MIME类型
     *
     * @param extension
     * @param allowedExtension
     * @return
     */
    private static boolean checkAllowedExtendName(String extension, String[] allowedExtension) {
        for (String str : allowedExtension) {
            if (str.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 文件大小检查
     * @param file
     * @param allowedExtension
     */
    public static FileCode checkFileSize(MultipartFile file, String[] allowedExtension){
        FileCode code = FileCode.FILE_SIZE_LIMIT;
        // 检查文件大小是否超限
        long size = file.getSize();
        if(allowedExtension == MimeTypeConstants.AVATAR_EXTENSION){
            if(size> FileConstants.DEFAULT_AVATAR_MAX_SIZE){
                code.setMsg(code.getMsg() + FileConstants.DEFAULT_AVATAR_MAX_SIZE / 1024 + "k");
                return code;
            }
        }else if(allowedExtension == MimeTypeConstants.IMAGE_EXTENSION){
            if(size>FileConstants.DEFAULT_IMAGE_MAX_SIZE){
                code.setMsg("图像" + code.getMsg() + FileConstants.DEFAULT_IMAGE_MAX_SIZE / 1024 / 1024 + "m");
                return code;
            }
        }else if(allowedExtension == MimeTypeConstants.MEDIA_EXTENSION){
            if(size>FileConstants.DEFAULT_MEDIA_MAX_SIZE){
                code.setMsg("媒体" + code.getMsg() + FileConstants.DEFAULT_MEDIA_MAX_SIZE / 1024 / 1024 + "m");
                return code;
            }
        }else if(allowedExtension == MimeTypeConstants.VIDEO_EXTENSION && size>FileConstants.DEFAULT_VIDEO_MAX_SIZE){
            code.setMsg("视频" + code.getMsg() + FileConstants.DEFAULT_MEDIA_MAX_SIZE / 1024 / 1024 + "m");
            return code;
        }
        return null;
    }

    /**
     * 获取上传的完整文件
     * @param uploadDir 目录
     * @param fileName 文件名
     * File.separator 的作用等同于 / （linux 下文件路劲分割符必须为 / windows为 \ 但 不区分 \ /
     * @return
     */
    public static File getUploadAbsoluteFile(String uploadDir, String fileName) {
        File desc = new File(uploadDir + File.separator + fileName);
        if (!desc.exists() && !desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return desc;
    }

}
